<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealName">
            <el-input v-model="form.dealName" :placeholder="$t('项目名称')" show-word-limit maxlength="100" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('签约主体')" prop="contractingEntity">
            <el-input v-model="form.contractingEntity" :placeholder="$t('签约主体')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('融资金额') + $t('金额单位')" prop="amountOfFinancing">
            <Money v-model="form.amountOfFinancing" :placeholder="$t('融资金额')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('融资摘要')" prop="financingSummary">
            <el-input v-model="form.financingSummary" type="textarea" :placeholder="$t('融资摘要')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="deal_financing_contract" :btn-show="!otherBtnHide" :business-id="autoBusinessId($route.query.id, form)" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="deal:financingContract" :menu-key="menuKey" :btn-hide="btnHide" />
    </div>
  </div>
</template>

<script>
    import { saveDealFinancingContract, getDealFinancingContract } from '@/api/deal/financingProjects/dealFinancingContract'
    import { getDealBaseFinancing } from '@/api/deal/financing/base/dealBaseFinancing'
    export default {
        data() {
            return {
              // 菜单标识
              menuKey: 'deal_financing_contract',
              // 是否隐藏保存按钮
              btnHide: false,
              // 是否隐藏其他按钮
              otherBtnHide: false,
                // 表单参数
                form: {
                  // 上传
                  fileList: []
                },
                // 表单校验
                rules: {
                    dealName: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    contractingEntity: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    amountOfFinancing: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    financingSummary: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    actTitle: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true
            }
        },
        created() {
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealFinancingContract(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                        this.loading = false
                    })
                } else {
                    this.reset()
                  // 获取项目名称
                  getDealBaseFinancing(this.$route.query.id).then(response => {
                    this.form.dealName = response.data.dealName
                  })
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    companyId: undefined,
                    dealName: undefined,
                    contractingEntity: undefined,
                    amountOfFinancing: undefined,
                    financingSummary: undefined,
                    actId: undefined,
                    actStatus: undefined,
                    actTitle: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                    this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm(type) {
                return new Promise((resolve, reject) => {
                  this.$refs['form'].validate(valid => {
                    // 验证数据
                    if (valid) {
                      // 弹出遮罩层
                      this.loading = true
                      // 保存数据
                      this.form.actTitle = '[' + this.form.dealName + '] 融资合同' + ':' + this.moneyFormat(this.form.amountOfFinancing, 'w') + this.$t('金额单位')
                      saveDealFinancingContract(this.form).then(response => {
                        if (response.code === 200) {
                          // 点击的按钮是保存并启动流程
                          if (type === 'process') {
                            // 启动流程
                            this.startProcess(response.data.id)
                            // 点击的仅保存
                          } else {
                            this.msgSuccess(this.$t('保存成功'))
                            // 关闭form页面
                            this.closeForm()
                          }
                          resolve()
                        } else {
                          this.msgError(response.msg)
                          this.loading = false
                          reject()
                        }
                      })
                    } else {
                      reject()
                      this.locationError(document)
                    }
                  })
                }).catch(() => {
                    this.loading = false
                })
           },
            // 启动流程
          startProcess: async function(dataId) {
             var dealName = this.form.dealName
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = '「' + dealName + '」 融资合同' + ':' + this.moneyFormat(this.form.amountOfFinancing, 'w') + this.$t('金额单位')
               // 业务表名
               processData.dataTable = 'deal_financing_contract'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/deal/financingProjects/financingContract/dealFinancingContractForm'
               processData.viewPath = '@/views/deal/financingProjects/financingContract/dealFinancingContractView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = this.menuKey
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           }

        }

    }
</script>
